﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using Job.Entity;
using Job.Logic.Company;
using Job.Web.Framework;
using System.IO;

namespace Job.Web.userControls.Company
{
    public partial class CompanyLogo : BaseCtrl
    {
        private int CompanyId
        {
            get
            {
                //后台管理员能通过后台为企业增加职位，所以在此处需要兼职企业ID从url或从Session中获取
                int id = myR["company"].IntValue;
                if (id > 0) return id;
                MySession session = new MySession();
                SessionCompany sCompany = session.GetCompanyFromSession();
                if (null == sCompany)
                    return 0;
                else
                    return sCompany.CompanyId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (0 == CompanyId)
            {
                Response.Redirect("CompanyLogin.aspx?categoryid=2&error=1");
                return;
            }

            if (!Page.IsPostBack)
            {
                JobCompanyInfo info = new CompanyLogic().GetLogo(CompanyId);
                if (info.CompanyLogo == null)
                    Image1.Visible = false;
                else
                    Image1.ImageUrl = WebCommon.GetApplicationPath(Request)
                        + "Handler/GetCompanyLogo.aspx?company=" + CompanyId + "&type=" + info.LogoExtName;
            }

            this.messageTd.Visible = false;
        }


        private void ShowMessage(string message)
        {
            this.messageTd.InnerHtml = "<font color='red'>" + message + "</font>";
            this.messageTd.Height = "30px";
            this.messageTd.Visible = true;
            this.messageTd.VAlign = "middle";
        }

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            if (this.fileUpload.PostedFile.FileName.Trim().Equals(""))
            {
                ShowMessage("指选择需要上传的Logo文件！");
                return;
            }

            string _regex = @"^.+\.(jpe?g|gif)$";
            Regex test = new Regex(_regex);

            if (!test.IsMatch(fileUpload.PostedFile.FileName))
            {
                ShowMessage("Logo文件只能为 JPG/JPEG 或 GIF 文件");
                return;
            }

            try
            {
                string extName = Path.GetExtension(fileUpload.PostedFile.FileName);
                JobCompanyInfo info = new JobCompanyInfo();
                info.CompanyInfoId = CompanyId;
                byte[] logo = Functions.StreamToByteArray(fileUpload.PostedFile.InputStream);
                info.CompanyLogo = logo;
                info.LogoExtName = extName;
                new CompanyLogic().UpdateLogo(info);

                ShowMessage("保存Logo成功！");
                Image1.ImageUrl = WebCommon.GetApplicationPath(Request)
                       + "Handler/GetCompanyLogo.aspx?company=" + CompanyId + "&type=" + extName;

            }
            catch (Exception ex)
            {
                ShowMessage("上传Logo失败：" + ex.Message);
            }
        }
    }
}